package com.learn.jackson.annotation.jsonSerialize;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldDefaults;

import java.io.IOException;

/**
 * @author XuYang
 * @since 2025/6/9 10:54
 */
@Data
@NoArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@AllArgsConstructor(staticName = "of")
public class JsonSerializeEntity {

    Long id;
    @JsonSerialize(
        using = NameSerializer.class
        // 通过指定类型来筛选序列化器
        , as = String.class
        // 处理空值序列化器
        , nullsUsing = JsonSerializer.None.class
    )
    String name;

    public static class NameSerializer
        extends JsonSerializer<String>
    {
        @Override
        public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
            gen.writeString("wrap:" + value);
        }
    }
}
